// personal.js

var app = getApp()
Page({
  data: {
    isSign: false,
    unPay:'',
    sent:'',
    sentto:'',
    finish:'',
    share_hidden:false
  },
  //待发货点击事件
  humpClick: function () {
    if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../myOrder/myOrder?activeIndex=2',
      })
    }
  },
  //已完成点击事件
  finishClick: function () {
    if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../myOrder/myOrder?activeIndex=3',
      })
    }
  },
  //待付款点击事件
  obligationClick: function () {
    if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../myOrder/myOrder?activeIndex=1',
      })
    }
  },
  //地址管理点击事件
  addrManageClick: function () {
    if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../addrManage/addrManage',
      })
    }
  },
  //我要当主播点击事件
  pushClick: function () {
    if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    }else{
      wx.navigateTo({
        url: '../liveroom/roomlist/roomlist',
      })
    }
  },
  //分享页面点击事件
  shareClick:function (){
    if(!app.globalData.inv_i){
      wx.navigateTo({
        url: '../login/login',
      })
    }else{
      wx.navigateTo({
        url: '../share/share',
      })
    }
  },
  //我的订单点击事件
  myOrderClick: function () {
    if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../myOrder/myOrder?activeIndex=0',
      })
    }
  },
  //登录测试时使用
  loginClick: function () {
    wx.navigateTo({
      url: '../login/login',
    })
  },
  /**
   * 退出登录
   */
  loginoutClick:function(){
    var thiz = this;
    wx.removeStorageSync('user_id')
    wx.removeStorageSync('nickName')
    app.globalData.inv_i = '';
    wx.switchTab({
      url: '../index/index',
    })
  },
  
  userInfo:function (options){
    var user_id = wx.getStorageSync('user_id');
    var url = app.globalData.domain + app.globalData.PageInterface;
    wx.request({
      url: url,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Personal',
        api: 7001,
        model: "MINI",
        user_id: user_id
      },
      success:function(res){      
        if(res.data.data){
          options.success && options.success({
            user_info: res.data.data
          })
        }else{
          options.fail && options.fail({
            user_info:res.data.msg
          })
        }
      }
    })
  },

  onShow: function () {
    var thiz = this
    var domain = app.globalData.domain
    var kui_key = app.globalData.kui_key
    var cid = app.globalData.cid
    var orderListInterface = app.globalData.PageInterface
    var user_id = app.globalData.inv_i
    //查看订单状态
    wx.request({
      url: domain + orderListInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Order',
        api: 8004,
        model : "MINI",
        user_id: user_id
      },
      success: function (res) {
        var finish = ''
        var unPay = ''
        var sent = ''
        var sentto = ''
        if (res.data.data) {
          unPay = res.data.data[0]
          sent = res.data.data[1]
          sentto = res.data.data[2]
          finish = res.data.data[3]
        }
        thiz.setData({
          finish: finish,
          unPay: unPay,
          sent: sent,
          sentto: sentto
        })
      }
    })
    //获取用户信息
    thiz.userInfo({
      success: function (res) {
        if (res.user_info.money) {
          thiz.setData({
            user : '余额：' + res.user_info.money,
            share_hidden : false,
            user_info : res.user_info
          })
        } else {
          thiz.setData({
            user: '',
            share_hidden: true,
            default_img:'../images/logo01.jpg'
          })
        }
      },
      fail: function (res) {
        //失败的操作
      }
    });
    //查询可评价商品
    // var payGoodsOrderInterface = app.globalData.payGoodsOrderInterface

    // wx.request({
    //   url: domain + payGoodsOrderInterface,
    //   method: 'POST',
    //   header: { 'content-type': 'application/x-www-form-urlencoded' },
    //   data: {
    //     c_id: cid,
    //     kui_key: kui_key,
    //     format_type: 'json',
    //     api_cate: 'Order',
    //     api: 4015,
    //     is_count: 1
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     thiz.setData({
    //       assessAmount: res.data.data.count,
    //     })
    //   }
    // })
    
  },

  onLoad: function (options) {
    var that = this
    var background_color = app.globalData.background_color
    app.login({
      share_id: options.share_id
    })
    wx.setNavigationBarColor({
      backgroundColor: background_color,
      frontColor: '#000000'
    })

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          background: background_color
        });
      }
    })
    that.setData({
      // userInfo: app.globalData.userInfo,
      distribution: false
    })
    // that.loadCommon()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '热马品牌商城小程序',
      path: "/pages/personal/personal?share_id=" + wx.getStorageSync('user_id'),
      imageUrl: "/pages/images/logo.png",
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功！',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})